package com.peanut.wapp.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.peanut.wapp.controller.req.BaseDataTablesReq;
import com.peanut.wapp.controller.req.CommitFeeParam;
import com.peanut.wapp.controller.res.BaseResult;
import com.peanut.wapp.controller.res.DataTablesResult;
import com.peanut.wapp.controller.res.PageResult;
import com.peanut.wapp.controller.resBean.RFeeBean;
import com.peanut.wapp.service.FeeService;
import com.peanut.wapp.service.exception.ServiceException;
import com.peanut.wapp.util.NumUtil;

@Controller
@RequestMapping(value ="/fee", produces = "application/json")
public class FeeController {
	Logger logger = Logger.getLogger(FeeController.class);

	@Autowired
	FeeService feeServiceImpl;
	
	@RequestMapping("/v1/commit")
	@ResponseBody
	public BaseResult commit(@Validated CommitFeeParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}
		int type = param.getType();
		int deliver = param.getDeliveryCycle();
		int confinement = param.getConfinementCycle();
		int value_add = param.getValueAdd();
		String phone = param.getPhone();
		String host = param.getHost();
		try {
			boolean flag = feeServiceImpl.commit(type, deliver, confinement, value_add, phone, host);
			res.setFlag(flag);
			
		} catch (ServiceException e) {
			res.setServiceException(e);
		}
		return res;
	}
	
	
	/**
	 * 获取用户列表
	 **/
	@RequestMapping("/v1/list")
	@ResponseBody
	public DataTablesResult<RFeeBean> list(BaseDataTablesReq param, HttpServletRequest request, HttpServletResponse response) {
		DataTablesResult<RFeeBean> result = new DataTablesResult<RFeeBean>();
		try {
			int start = NumUtil.toInt(param.getStart(), 0);
			int pageSize = NumUtil.toInt(param.getLength(), 10);
			String key = request.getParameter("key");
			PageResult<RFeeBean> resData = feeServiceImpl.getList(key, start, pageSize);
			if (resData != null) {
		        result.setData(resData.getData());
		        result.setDraw(param.getDraw()); 
		        result.setRecordsTotal(resData.getCount());
		        result.setRecordsFiltered(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "获取异常"));
		}
		return result;
	}
	
}
